package leetcode_top;

public class Ex13 {
    class Solution {
        public int romanToInt(String s) {
            return dfs(s.toCharArray(), 0, 0);
        }

        public int dfs(char[] cs, int idx, int cur) {
            if (idx == cs.length) {
                return cur;
            }
            if (cs[idx] == 'I') {
                if (idx + 1 < cs.length && cs[idx + 1] == 'X') {
                    return dfs(cs, idx + 2, cur + 9);
                } else if (idx + 1 < cs.length && cs[idx + 1] == 'V') {
                    return dfs(cs, idx + 2, cur + 4);
                } else {
                    return dfs(cs, idx + 1, cur + 1);
                }
            } else if (cs[idx] == 'X') {
                if (idx + 1 < cs.length && cs[idx + 1] == 'C') {
                    return dfs(cs, idx + 2, cur + 90);
                } else if (idx + 1 < cs.length && cs[idx + 1] == 'L') {
                    return dfs(cs, idx + 2, cur + 40);
                } else {
                    return dfs(cs, idx + 1, cur + 10);
                }
            } else if (cs[idx] == 'C') {
                if (idx + 1 < cs.length && cs[idx + 1] == 'M') {
                    return dfs(cs, idx + 2, cur + 900);
                } else if (idx + 1 < cs.length && cs[idx + 1] == 'D') {
                    return dfs(cs, idx + 2, cur + 400);
                } else {
                    return dfs(cs, idx + 1, cur + 100);
                }
            } else if (cs[idx] == 'V') { 
                return dfs(cs, idx + 1, cur + 5);
            } else if (cs[idx] == 'L') { 
                return dfs(cs, idx + 1, cur + 50);
            }  else if (cs[idx] == 'D') { 
                return dfs(cs, idx + 1, cur + 500);
            } else {
                return dfs(cs, idx + 1, cur + 1000);
            }
        }
    }
}
